Skip to content

The synchronous ToList reads into pooled chunks, as ToListAsync does - #287

Merged
electricessence merged 1 commit into
masterfrom
feature/sync-tolist-chain
Dec 2, 2026
Merged

electricessence merged 1 commit into
masterfrom
feature/sync-tolist-chain

Conversation

@electricessence

Copy link
Copy Markdown
Collaborator

The synchronous ToList() of a view or a reader now reads into the pooled chunk chain and copies once, as ToListAsync does.

It grew a list of its own: the call allocated the growth as well as the result, and rented no pooled array, so the copy-out clearing of the previous change never reached it. It now writes its rows straight into the chain (its own loop, like the synchronous ToArray, so there is no sink indirection on the hot path) and copies them once into a list of exactly their number. A bare IDataReader is read through Fill into the same chain, as ToArray does it. No public API changes. It was the only collector that grew its own list.

Bytes per call

GC.GetAllocatedBytesForCurrentThread around one call of table.Map<int>().ToList() over an in-memory DataTable of int rows, warmed first, the smallest of 15 runs, in Release. No timing.

Rows ToList() before ToList() after a list that grows, by hand synchronous ToArray()
0 784 784 760 752
1 824 816 800 784
1,000 33,152 28,784 33,128 28,752
100,000 1,049,728 400,808 1,049,704 400,776

ToList is now an array plus the list object (32 bytes) at every size, and below the old shape from one row up. The probe SyncToListAllocationProbe.Probe_BytesPerCall prints the table (explicit); a Release-only test pins that the call allocates less than a list that grows at 1,000 and 100,000 rows (the bytes are exact, so a revert to the old shape, which measures 24 bytes above the hand-written loop, fails it). The timing of chain plus clear against the old list goes in the next timed run.

Tests

  • Every row in order and an exact capacity, from none to 20,000 rows (the old list had spare capacity at most of those sizes).
  • Skip and Take; a take of zero rents nothing.
  • Every pooled array goes back, and clean, with the copy-out clearing, and when a row fails mid-read.
  • A bare IDataReader reads into the same chain, with an exact capacity; a mapper that throws on the third row gives the chain back (checked on the parked chain of a row type no other test uses) and its own exception comes out unchanged, on the DbDataReader fast path and on the bare path.
  • The synchronous ToList joins the terminals of the copy-out clearing tests, for no pool offered, UsePool(true) and UsePool(false). It failed them before this change, because it rented nothing.

Checks

  • Core tests: 15663 pass in Release with the generator on; 15642 pass (16 skipped) in Debug with it off.
  • 231 sample checks pass; the Visual Studio solution build has no errors; dotnet format style and analyzers at --severity info report nothing on all 14 projects; the docs build is clean.
  • Reviewed by two Haiku reviewers (correctness and behavior preservation, test adequacy), arbitrated. The correctness review found no defect (disposal on every path, the row-failure index, the abandon steps and the empty and exact-capacity results all match the old code and the sibling ToArray). Real finding from the test review fixed: the bare IDataReader path had no failure test. Not taken: a fixed byte budget for the allocation test (the byte counts are deterministic and the old shape fails the comparison), and removing the !DEBUG gate (it is the pattern of the existing ToListAsync test).

Release notes under "Changes since rc.16".

🤖 Generated with Claude Code

On a view or a reader, the synchronous `ToList()` grew a list of its own: it allocated the growth as well as the result and rented no pooled array, so the copy-out clearing never reached it. It now writes its rows straight into the chunk chain (its own loop, like the synchronous `ToArray`) and copies them once into a list of exactly their number. A bare `IDataReader` is read through `Fill` into the same chain, as `ToArray` does. No public API changes.

Bytes per call, a mapped `DataTable` of `int` rows (0, 1, 1,000, 100,000 rows): `ToList()` 784, 824, 33,152 and 1,049,728 before; 784, 816, 28,784 and 400,808 after. The synchronous `ToArray` is 752, 784, 28,752 and 400,776, so `ToList` is now an array plus the list object. A Release-only test pins that it allocates less than a list that grows; an explicit probe prints the numbers.

Tests: every row in order and an exact capacity (none to 20,000), skip and take, a take of zero rents nothing, every array goes back clean, a failure mid-read returns the chain on both the fast path and the bare `IDataReader` path, and a bare `IDataReader`; the synchronous `ToList` joins the terminals of the copy-out clearing tests (it failed them before: it rented nothing).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit b11b571 into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the feature/sync-tolist-chain branch December 2, 2026 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant